package com.appheader.rongmaphone.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.appheader.rongmaphone.R;
import com.appheader.rongmaphone.fragment.UserSecureFragment;
import com.appheader.rongmaphone.fragment.UserSetFragment;


public class MySettingActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView imageView;
    private LinearLayout setBackImg;
    private RelativeLayout clearData;
    private TextView showCacheData;
    private TextView userLogout;
    private RadioGroup radioGroup;
    private FragmentManager manager;
    private UserSetFragment userSetFragment;
    private UserSecureFragment secureFragment;
    private RadioButton radioUserSet,radioUserSecure;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_my_setting);
        initView();


        //刚进入 “个性设置”选项先加载
        manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        hideFragment(transaction);
        transaction.commit();
        showUserSet(transaction);
        setRadioBtnListener();
        setListener();
    }

    private void setListener() {
        setBackImg.setOnClickListener(this);
    }

    private void setRadioBtnListener() {
        manager = getSupportFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction = manager.beginTransaction();
                hideFragment(transaction);
                switch (checkedId){
                    case R.id.radio_user_set:
                        radioGroup.setBackground(getResources().getDrawable(R.drawable.set_rg_shape));
                        showUserSet(transaction);
                        radioUserSet.setBackground(getResources().getDrawable(R.drawable.rb_shape));
                        break;
                    case R.id.radio_user_secure:
                        radioGroup.setBackground(getResources().getDrawable(R.drawable.secure_rg_shape));
                        showUserSecure(transaction);
                        radioUserSet.setBackground(getResources().getDrawable(R.drawable.secure_rg_shape));
                        break;
                }
                transaction.commit();
            }
        });
    }

    private void showUserSecure(FragmentTransaction transaction) {
        if (secureFragment == null){
            secureFragment = new UserSecureFragment();
            transaction.add(R.id.set_frame_layout,secureFragment);
        }else {
            transaction.show(secureFragment);
        }
    }

    private void showUserSet(FragmentTransaction transaction) {
        if (userSetFragment == null){
            userSetFragment = new UserSetFragment();
            transaction.add(R.id.set_frame_layout,userSetFragment);
        }else {
            transaction.show(userSetFragment);
        }
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (userSetFragment!=null){
            transaction.hide(userSetFragment);
        }
        if (secureFragment!=null){
            transaction.hide(secureFragment);
        }
    }

    private void initView() {
        setBackImg = (LinearLayout) findViewById(R.id.set_back_img);
        radioGroup = (RadioGroup) findViewById(R.id.setting_group);
        radioUserSet = (RadioButton) findViewById(R.id.radio_user_set);
        radioUserSecure = (RadioButton) findViewById(R.id.radio_user_secure);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.set_back_img://返回
                finish();
                break;
        }
    }
//
//    @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == 0 && resultCode == RESULT_OK) {
//            Uri uri = data.getData();
//            cropImage(uri, 100, 100, 3);
//        } else if (requestCode == 1 && resultCode == RESULT_OK) {
//            Uri picUri = data.getData();
//            cropImage(picUri, 100, 100, 3);
//        } else if (requestCode == 3 && resultCode == RESULT_OK) {
//            Uri data1 = data.getData();
//            if (data1 == null) {
//                Bitmap bitmap = (Bitmap) data.getExtras().get("data");
//                if (bitmap != null) {
//                    imageView.setImageBitmap(bitmap);
//                }
//
//            } else {
//                Bitmap bitmap = BitmapFactory.decodeFile(data1.toString());
//                if (bitmap != null) {
//                    imageView.setImageBitmap(bitmap);
//                }
//            }
//        }
//    }

    //    private String getAppCacheSize() {
//
//        try {
////            Log.i("InternalCacheDir; ExternalCacheDir", DataCleanManager.getCacheSize(getActivity().getApplicationContext().getCacheDir()) + "; "+DataCleanManager.getCacheSize(getActivity().getApplicationContext().getExternalCacheDir()));
//
//            long cacheSize = DataCleanManager.getFolderSize(getActivity().getApplicationContext().getCacheDir()) +
//                    DataCleanManager.getFolderSize(getActivity().getApplicationContext().getExternalCacheDir());
//
//            return DataCleanManager.getFormatSize(cacheSize);
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//        }
//        return "--B";
//    }
    //截取图片
//    public void cropImage(Uri uri, int outputX, int outputY, int requestCode) {
//        //裁剪图片意图
//        Intent intent = new Intent("com.android.camera.action.CROP");
//        intent.setDataAndType(uri, "image/*");
//        intent.putExtra("crop", "true");
//        //裁剪框的比例，1：1是个圆形的，如果是2:1是个矩形的
//        intent.putExtra("aspectX", 1);
//        intent.putExtra("aspectY", 1);
//        //裁剪后输出图片的尺寸大小
//        intent.putExtra("outputX", outputX);
//        intent.putExtra("outputY", outputY);
//        //图片格式
//        intent.putExtra("outputFormat", "JPEG");
//        intent.putExtra("noFaceDetection", true);
//        intent.putExtra("return-data", true);
//        startActivityForResult(intent, requestCode);
//    }

//    @Override
//    public void onClick(View v) {
//        switch (v.getId()) {
//            case R.id.set_back_img://返回
//                finish();
//                break;
//            case R.id.user_img:
//                AlertDialog.Builder builder = new AlertDialog.Builder(MySettingActivity.this);
//                builder.setTitle("选择方式");
//
//                builder.setItems(new String[]{"从手机本地", "拍照"}, new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                        switch (which) {
//                            case 0:
//
//                                Intent intentBendi = new Intent(Intent.ACTION_PICK);
//                                //设置获取图片的uri和类型
//                                intentBendi.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
//                                startActivityForResult(intentBendi, 1);
//                                break;
//                            case 1:
//
//                                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//                                //保存在一个额外路径
////                                intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(path)));
//
//                                startActivityForResult(intent, 0);
//                                break;
//                        }
//                    }
//                });
//
//                builder.show();
//                break;
//            case R.id.clear_data:
//                AlertDialog.Builder builder1 = new AlertDialog.Builder(MySettingActivity.this);
//                builder1.setMessage("确定要删除缓存吗");
//                builder1.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                });
//                //在这里做清楚缓存的动作
//                builder1.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                        List<String> list = DbUtil.selectUpLoadPic();
//                        for (int i = 0; i < list.size(); i++) {
//                            File file = new File(list.get(i));
//                            if (file.exists() && file.isFile()) {
//                                file.delete();
//                            }
//                        }
//
//                    }
//                });
//
//                builder1.show();
//                break;
//            case R.id.user_logout://用户退出登录
//
//                AlertDialog.Builder logoutBuilder = new AlertDialog.Builder(MySettingActivity.this);
//                logoutBuilder.setMessage("确定要退出吗");
//                logoutBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                    }
//                });
//                //在这里做
//                logoutBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//
//                        //清楚当前应用
//                        CurrentUserManager.clearCurrentUser();
//                        //退出所有的activity
//                        ActivityManager.getInstance().popAllActivity();
//
////                        finish();
//                    }
//                });
//
//                logoutBuilder.show();
//                break;
//        }
//    }
}
